package com.jinwei.qunguanjia.web.user.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.utils.FileUtils;
import com.jinwei.qunguanjia.common.utils.QiniuUtils;
import com.jinwei.qunguanjia.common.utils.SystemContextUtils;
import com.jinwei.qunguanjia.user.dto.UserDTO;
import com.jinwei.qunguanjia.user.service.UserService;
import com.jinwei.qunguanjia.web.group.vo.GroupVO;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * OCR相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/ocr")
public class OcrController {

    @Resource
    private Tesseract tesseract;

    @Resource
    private UserService userService;

    /**
     * 个人中心识别
     * @param file 文件流
     */
    @PostMapping("/personalCenter")
    public Response<String> personalCenter(@RequestParam("file") MultipartFile file) throws IOException, TesseractException {
        // 转换
        InputStream sbs = new ByteArrayInputStream(file.getBytes());
        BufferedImage bufferedImage = ImageIO.read(sbs);
        // 对图片进行文字识别
        String content = tesseract.doOCR(bufferedImage);
        String[] array = content.split("\n");
        String result ="";
        boolean flag = false;
        for (String string : array) {
            if (string.contains("做 信 号 ")) {
                result = string.replace("做 信 号 ","");
                flag = true;
                break;
            }
        }
        if (!flag) {
            return Response.fail("上传图片有误");
        }
        UserDTO userQueryDTO = userService.get(SystemContextUtils.getUserId());
        if (result.equals(userQueryDTO.getWeChatNumber())) {
            UserDTO userDTO = new UserDTO();
            userDTO.setUserId(userQueryDTO.getUserId());
            userDTO.setWeChatNumberOcr(result);
            userDTO.setWeChatStatus("1");
            //上传图片
            String objectName = FileUtils.getStoragePath(file.getOriginalFilename());
            QiniuUtils.upload(file.getBytes(), objectName);
            userDTO.setPersonalCenterUrl(objectName);
            userService.update(userDTO);
            return Response.success(result);
        } else {
            return Response.fail("识别的微信号与输入的微信号不一致");
        }
    }

    /**
     * 群信息识别
     * @param file 文件流
     */
    @PostMapping("/groupInfo")
    public Response<GroupVO> groupInfo(@RequestParam("file") MultipartFile file) throws IOException, TesseractException {
        // 转换
        InputStream sbs = new ByteArrayInputStream(file.getBytes());
        BufferedImage bufferedImage = ImageIO.read(sbs);
        // 对图片进行文字识别
        String content = tesseract.doOCR(bufferedImage);
        String[] array = content.split("\n");
        String groupName  = "";
        String groupPeopleNumber = "";
        for (String string : array) {
            if (string.contains("聊 天 信 息")) {
                groupPeopleNumber = string.replace("< 聊 天 信 息 (","").replace(")","");
            } else if (string.contains("群 聊 名 称")) {
                groupName = string.replace("群 聊 名 称 ","").replace(" ","");
            }
        }
        GroupVO groupVO = new GroupVO();
        groupVO.setGroupName(groupName);
        groupVO.setGroupPeopleNumber(groupPeopleNumber);
        return Response.success(groupVO);
    }
}
